package com.qqtech.qquan.community.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.enums.CommunityJoinStatusDictEnum;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.model.CommunityJoin;
import com.qqtech.qquan.community.model.CommunityJoinQuery;
import com.qqtech.qquan.community.model.CommunityJoinVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.community.service.CommunityJoinService;
import com.qqtech.qquan.community.service.CommunitySysParamService;
import com.qqtech.qquan.push.service.PushAudienceService;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/community/join")
public class CommunityJoinController extends BaseWebController<CommunityJoin, CommunityJoinQuery> {
	@Resource
	private CommunityJoinService communityJoinService;

	@Resource
	private CommunityBaseService communityBaseService;

	@Resource
	private CommunitySysParamService communitySysParamService;

	@Resource
	private UserBaseService userBaseService;

	@Resource
	private PushAudienceService pushAudienceService;

	@Override
	protected BaseService<CommunityJoin> getBaseService() {
		return communityJoinService;
	}

	/**
	 * 我的加入列表
	 * 
	 * @param pubConcern
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myList")
	@ResponseBody
	public Result myList() {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		UserBase userBase = userBaseService.getById(curUserId);
		CommunityBaseVo communityBaseVo = communityBaseService.getById(userBase.getCommunityId());
		result.put("communityBaseVo", communityBaseVo);
		return result;
	}

	/**
	 * 我的加入列表
	 * 
	 * @param pubConcern
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/list")
	@ResponseBody
	public Result list(@RequestBody CommunityJoinQuery communityJoinQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		// 获取加入社区list
		communityJoinQuery.setUserId(curUserId);
		communityJoinQuery.setStatus(CommunityJoinStatusDictEnum.NORMAL.getCode());
		// 获取加入社区list
		List<CommunityJoinVo> list = communityJoinService.queryList(communityJoinQuery);
		if (list.size() > 0) {
			CommunityBaseVo joinBaseVo = null;
			for (CommunityJoinVo communityJoinVo : list) {
				joinBaseVo = communityBaseService.getById(communityJoinVo.getCommunityId());
				communityJoinVo.setCommunityName(joinBaseVo.getName());
			}
		}
		result.put("list", list);
		return result;
	}

	/**
	 * 获取加入的社区list
	 * 
	 * @param id
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/joinList")
	@ResponseBody
	public Result joinList() {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		List<CommunityBaseVo> list = new ArrayList<CommunityBaseVo>();
		UserBase userBase = userBaseService.getById(curUserId);
		if (null != userBase.getCommunityId()) {
			CommunityBaseVo communityBaseVo = communityBaseService.getById(userBase.getCommunityId());
			list.add(communityBaseVo);
		}
		result.put("communityList", list);
		return result;
	}

	/**
	 * 是否加入某个社区
	 * 
	 * @param pubConcern
	 * @return
	 */
	@RequestMapping(value = "/isJoin")
	@ResponseBody
	public Result isJoin(@RequestBody CommunityJoinQuery communityJoinQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		boolean isJoin = false;
		if (null != curUserId) {
			isJoin = communityJoinService.isJoinCommunity(communityJoinQuery.getCommunityId(), curUserId);
		}
		result.put("isJoin", isJoin);
		return result;
	}

	/**
	 * 是否有社区
	 * 
	 * @param pubConcern
	 * @return
	 */
	@RequestMapping(value = "/hasJoin")
	@ResponseBody
	public Result hasJoin() {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		boolean hasJoin = false;
		if (null != curUserId) {
			List<Integer> communityIds = communityJoinService.queryMyCommuntiyIds(curUserId);
			if (null != communityIds && communityIds.size() > 0) {
				hasJoin = true;
			}
		}
		result.put("hasJoin", hasJoin);
		return result;
	}

	/**
	 * 加入社区
	 * 
	 * @param pubConcern
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(@RequestBody CommunityJoin communityJoin) {
		Result result = new Result();
		// 查看是否已存在加入信息
		Integer curUserId = super.getUserId();
		result = join(communityJoin.getCommunityId(), result, curUserId);
		return result;
	}

	private Result join(Integer communityId, Result result, Integer curUserId) {
		UserBase userBase = userBaseService.getById(curUserId);
		Integer oldCommunityId = userBase.getCommunityId();
		boolean isOfficial = false;
		if (userBase.getIsOfficial() != null && YesNoEnum.YES.getCode() == userBase.getIsOfficial().intValue()) {
			isOfficial = true;
		}
		Timestamp nowTime = TimeUtil.now();
		int month = 0;
		if (null != userBase.getCommunityId() && communityId.intValue() == userBase.getCommunityId()) {
			result.addErr("已加入当前社区，无需重复加入。");
			return result;
		}
		CommunityJoinQuery query = new CommunityJoinQuery();
		query.setUserId(curUserId);
		query.setStatus(CommunityJoinStatusDictEnum.QUIT.getCode());
		int quitCount = 0;
		if (isOfficial == false) {
			quitCount = communityJoinService.queryCount(query);
			if (quitCount >= 3) {// 退出过3次社区
				query.setStatus(CommunityJoinStatusDictEnum.NORMAL.getCode());
				CommunityJoin communityJoin = communityJoinService.getFirstOne(query, new PageOrder());
				month = (nowTime.getYear() - communityJoin.getNewAt().getYear()) * 12 + nowTime.getMonth()
						- communityJoin.getNewAt().getMonth();
				if (month < 1) {
					result.addErr("加入社区小于1个月，不允许切换社区。");
					return result;
				}
			}
		}
		int joinCount = communityJoinService.handleJoin(curUserId, communityId);
		result.put("joinCount", joinCount);
		if (isOfficial == false) {
			query.setStatus(CommunityJoinStatusDictEnum.QUIT.getCode());
			quitCount = communityJoinService.queryCount(query);
			if (quitCount < 3) {
				result.put("joinMessage", "还允许不满1个月切换" + (3 - quitCount) + "次社区。");
			}
		}
		pushAudienceService.updateTagAlias(curUserId, communityId, oldCommunityId);
		return result;
	}

	@RequestMapping(value = "/face2community")
	@ResponseBody
	public Result face2community(@RequestBody Map<String, String> map) {
		Result result = new Result();
		// 查看是否已存在加入信息
		Integer curUserId = super.getUserId();
		String bizzId = map.get("bizzId").toString();
		Integer communityId = Integer.parseInt(bizzId);
		result = join(communityId, result, curUserId);
		return result;
	}
}
